package com.bci.pwtz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.VdystatMapper;
import com.bci.pwtz.mysql.model.Vdystat;
import com.bci.pwtz.mysql.model.Vzystat;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.VdystatService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class VdystatServiceImpl implements VdystatService
{

    
    private VdystatMapper objMapper;
    
    private CompanyService companyService;
    
    /**
     * @return the objMapper
     */
    public VdystatMapper getObjMapper()
    {
        return objMapper;
    }

    /**
     * @param objMapper the objMapper to set
     */
    public void setObjMapper(VdystatMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    /**
     * @return the companyService
     */
    public CompanyService getCompanyService()
    {
        return companyService;
    }

    /**
     * @param companyService the companyService to set
     */
    public void setCompanyService(CompanyService companyService)
    {
        this.companyService = companyService;
    }

    private static final Logger _logger = Logger.getLogger(VdystatServiceImpl.class);
    
    @Override
    public List<Vdystat> findListByUserId(Long companyId,int type) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("findVdystat:|companyId=" + companyId);
        }
        List<Vdystat> list = null;
        List<Vdystat> newlist = new ArrayList<Vdystat>();
        String companyidStr = "";
        String[] companyids=null;
        companyidStr = companyService.findChildrensId(companyId,null);
        if(companyidStr!=null && companyidStr.length()>0){
            companyidStr= companyidStr.substring(0,companyidStr.length()-1);
            companyids =companyidStr.split(",");
        }
        try
        {
            list = objMapper.selectListByUserId(companyId, companyids);
            Vdystat vds= null;
            if(list.size()>0){
                if(type==1){
                   String cids=companyService.findChildren(companyId);
                   if(cids==""){
                	   newlist.addAll(list);
                   }else{
                	   String[] ids =null;
                       if(cids!=null)
                           ids = cids.split(",");
                       for(String id:ids){
                           String cids1=companyService.findChildrensId(PwtzUtils.passLong(id,-1),null);
                           String[] ids1 =null;
                           if(cids1!=null)
                               ids1 = cids1.split(",");
                           vds = findList(list,ids1);
                           if(vds!=null && vds.getCompanyId()!=null)
                           newlist.add(vds);
                       }
                       //统计本单位数据进统计
                       //vds = findList(list,null);
                       //newlist.add(vds);
                       for(Vdystat v:list){
                           if(companyId==v.getCompanyId()){
                               newlist.add(v);
                           }
                       }
                   }
                }else if(type==0){
                    vds = findList(list,null);
                    if(vds!=null && vds.getCompanyId()!=null)
                    newlist.add(vds);
                }
            }
            
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findVdystat:result list=" + list.toString());
        }

        return newlist;
    }
    
    private Vdystat findList(List<Vdystat> list,String[] ids) throws PwtzDatabaseException
    {
        Vdystat vds= new Vdystat();;
        try
        {
            if(list.size()>0){
                Long   alldyxlcount                  =0L;
                Double alldyxltotallinelength        =0D;
                Double alldyxloverheadlinelength     =0D;
                Double alldyxloverheadlinelengthBar  =0D;
                Double alldyxloverheadlinelengthCover=0D;
                Double alldyxlcablelength            =0D;
                Long   alldygtcount                  =0L;
                Long   alllxcount                    =0L;
                Long   allpublicbyqcount             =0L;
                Double allpublicbyqcapacity          =0D;
                Long   allprivatebyqcount            =0L;
                Double allprivatebyqcapacity         =0D;
                Long   alldypdxcount                 =0L;
                Long   alldydlfjxcount               =0L;
                Long   alldyzsrdqcount               =0L;
                Long   alldyzskgcount                =0L;
                Long   alldywgbczzcount              =0L;
                Long   alldyzsblqcount               =0L;
                Long   alldyrsxcount                 =0L;
                Long   alldyzsdrqcount               =0L;
                Double alldyzsdrqcapacity            =0D;
                int count=0;
                for(int i=0;i<list.size();i++){
                    if(ids==null){
                        vds.setCompanyName(list.get(0).getCompanyName());
                        alldyxlcount                   +=list.get(i).getDyxlcount()                  ;
                        alldyxltotallinelength         +=list.get(i).getDyxltotallinelength()        ;
                        alldyxloverheadlinelength      +=list.get(i).getDyxloverheadlinelength()     ;
                        alldyxloverheadlinelengthBar   +=list.get(i).getDyxloverheadlinelengthBar()  ;
                        alldyxloverheadlinelengthCover +=list.get(i).getDyxloverheadlinelengthCover();
                        alldyxlcablelength             +=list.get(i).getDyxlcablelength()            ;
                        alldygtcount                   +=list.get(i).getDygtcount()                  ;
                        alllxcount                     +=list.get(i).getLxcount()                   ;
                        allpublicbyqcount              +=list.get(i).getPublicbyqcount()             ;
                        allpublicbyqcapacity           +=list.get(i).getPublicbyqcapacity()          ;
                        allprivatebyqcount             +=list.get(i).getPrivatebyqcount()            ;
                        allprivatebyqcapacity          +=list.get(i).getPrivatebyqcapacity()         ;
                        alldypdxcount                  +=list.get(i).getDypdxcount()                 ;
                        alldydlfjxcount                +=list.get(i).getDydlfjxcount()               ;
                        alldyzsrdqcount                +=list.get(i).getDyzsrdqcount()               ;
                        alldyzskgcount                 +=list.get(i).getDyzskgcount()                ;
                        alldywgbczzcount               +=list.get(i).getDywgbczzcount()              ;
                        alldyzsblqcount                +=list.get(i).getDyzsblqcount()               ;
                        alldyrsxcount                  +=list.get(i).getDyrsxcount()                 ;
                        alldyzsdrqcount                +=list.get(i).getDyzsdrqcount()               ;
                        alldyzsdrqcapacity             +=list.get(i).getDyzsdrqcapacity()            ;
                    }else{
                        for(String id:ids){
                            if(id.equals(list.get(i).getCompanyId().toString())){
                                if(count ==0){
                                    vds.setCompanyName(list.get(i).getCompanyName());
                                }
                                count ++;
                                alldyxlcount                   +=list.get(i).getDyxlcount()                  ;
                                alldyxltotallinelength         +=list.get(i).getDyxltotallinelength()        ;
                                alldyxloverheadlinelength      +=list.get(i).getDyxloverheadlinelength()     ;
                                alldyxloverheadlinelengthBar   +=list.get(i).getDyxloverheadlinelengthBar()  ;
                                alldyxloverheadlinelengthCover +=list.get(i).getDyxloverheadlinelengthCover();
                                alldyxlcablelength             +=list.get(i).getDyxlcablelength()            ;
                                alldygtcount                   +=list.get(i).getDygtcount()                  ;
                                alllxcount                     +=list.get(i).getLxcount()                   ;
                                allpublicbyqcount              +=list.get(i).getPublicbyqcount()             ;
                                allpublicbyqcapacity           +=list.get(i).getPublicbyqcapacity()          ;
                                allprivatebyqcount             +=list.get(i).getPrivatebyqcount()            ;
                                allprivatebyqcapacity          +=list.get(i).getPrivatebyqcapacity()         ;
                                alldypdxcount                  +=list.get(i).getDypdxcount()                 ;
                                alldydlfjxcount                +=list.get(i).getDydlfjxcount()               ;
                                alldyzsrdqcount                +=list.get(i).getDyzsrdqcount()               ;
                                alldyzskgcount                 +=list.get(i).getDyzskgcount()                ;
                                alldywgbczzcount               +=list.get(i).getDywgbczzcount()              ;
                                alldyzsblqcount                +=list.get(i).getDyzsblqcount()               ;
                                alldyrsxcount                  +=list.get(i).getDyrsxcount()                 ;
                                alldyzsdrqcount                +=list.get(i).getDyzsdrqcount()               ;
                                alldyzsdrqcapacity             +=list.get(i).getDyzsdrqcapacity()            ;
                            }
                        }
                    }
                }
                
                vds.setDyxlcount(alldyxlcount                  );
                vds.setDyxltotallinelength(alldyxltotallinelength        );
                vds.setDyxloverheadlinelength(alldyxloverheadlinelength     );
                vds.setDyxloverheadlinelengthBar(alldyxloverheadlinelengthBar  );
                vds.setDyxloverheadlinelengthCover(alldyxloverheadlinelengthCover);
                vds.setDyxlcablelength(alldyxlcablelength            );
                vds.setDygtcount(alldygtcount                  );
                vds.setLxcount(alllxcount                    );
                vds.setPublicbyqcount(allpublicbyqcount             );
                vds.setPublicbyqcapacity(allpublicbyqcapacity          );
                vds.setPrivatebyqcount(allprivatebyqcount            );
                vds.setPrivatebyqcapacity(allprivatebyqcapacity         );
                vds.setDypdxcount(alldypdxcount                 );
                vds.setDydlfjxcount(alldydlfjxcount               );
                vds.setDyzsrdqcount(alldyzsrdqcount               );
                vds.setDyzskgcount(alldyzskgcount                );
                vds.setDywgbczzcount(alldywgbczzcount              );
                vds.setDyzsblqcount(alldyzsblqcount               );
                vds.setDyrsxcount(alldyrsxcount                 );
                vds.setDyzsdrqcount(alldyzsdrqcount               );
                vds.setDyzsdrqcapacity(alldyzsdrqcapacity            );
            }
            
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findVdystat:result list=" + list.toString());
        }

        return vds;
    }
}
